package com.settlement.system.service.impl;

import com.settlement.system.model.entity.OdsOmsRetailSalesOrderMain;
import com.settlement.system.mapper.OdsOmsRetailSalesOrderMainMapper;
import com.settlement.system.service.OdsOmsRetailSalesOrderMainService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.model.query.OdsOmsRetailSalesOrderMainQuery;
import com.settlement.system.model.form.OdsOmsRetailSalesOrderMainForm;
import com.settlement.system.model.vo.OdsOmsRetailSalesOrderMainImportVo;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;
import com.settlement.system.common.util.StringUtils;

/**
 * <p>
 * oms订单表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-08-02
 */
@Service
public class OdsOmsRetailSalesOrderMainServiceImpl extends ServiceImpl<OdsOmsRetailSalesOrderMainMapper, OdsOmsRetailSalesOrderMain> implements OdsOmsRetailSalesOrderMainService {

    @Override
    public Page<OdsOmsRetailSalesOrderMain> getPage(OdsOmsRetailSalesOrderMainQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<OdsOmsRetailSalesOrderMain> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

     @Override
     public boolean saveOdsOmsRetailSalesOrderMain(OdsOmsRetailSalesOrderMainForm odsOmsRetailSalesOrderMainForm) {
         OdsOmsRetailSalesOrderMain odsOmsRetailSalesOrderMain = new OdsOmsRetailSalesOrderMain();
         BeanUtils.copyProperties(odsOmsRetailSalesOrderMainForm, odsOmsRetailSalesOrderMain);
         return this.saveOrUpdate(odsOmsRetailSalesOrderMain);
     }

     @Override
     public OdsOmsRetailSalesOrderMainForm getOdsOmsRetailSalesOrderMainFormData(Long id) {
         OdsOmsRetailSalesOrderMain odsOmsRetailSalesOrderMain = this.getById(id);
         OdsOmsRetailSalesOrderMainForm odsOmsRetailSalesOrderMainForm = new OdsOmsRetailSalesOrderMainForm();
         BeanUtils.copyProperties(odsOmsRetailSalesOrderMain, odsOmsRetailSalesOrderMainForm);
         return odsOmsRetailSalesOrderMainForm;
     }

     @Override
     public boolean deleteOdsOmsRetailSalesOrderMain(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
     }

    @Override
    public void importData(List<OdsOmsRetailSalesOrderMainImportVo> voList)  {
        List<OdsOmsRetailSalesOrderMain> odsOmsRetailSalesOrderMainList = new ArrayList<>();
        for (OdsOmsRetailSalesOrderMainImportVo importVo : voList) {
            OdsOmsRetailSalesOrderMain odsOmsRetailSalesOrderMain = new OdsOmsRetailSalesOrderMain();
            BeanUtils.copyProperties(importVo, odsOmsRetailSalesOrderMain);
            odsOmsRetailSalesOrderMainList.add(odsOmsRetailSalesOrderMain);
        }
        this.saveOrUpdateBatch(odsOmsRetailSalesOrderMainList);
    }

    @Override
    public List<OdsOmsRetailSalesOrderMainImportVo> findByIds(String idsStr) {
        if (!StringUtils.hasText(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<OdsOmsRetailSalesOrderMain> odsOmsRetailSalesOrderMainList = this.getBaseMapper().selectBatchIds(ids);
        List<OdsOmsRetailSalesOrderMainImportVo> voList = new ArrayList<>();
        for (OdsOmsRetailSalesOrderMain odsOmsRetailSalesOrderMain : odsOmsRetailSalesOrderMainList) {
            OdsOmsRetailSalesOrderMainImportVo vo = new OdsOmsRetailSalesOrderMainImportVo();
            BeanUtils.copyProperties(odsOmsRetailSalesOrderMain, vo);
            voList.add(vo);
        }
        return voList;
    }
}
